//
//  TransferGroupOwnerViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/22.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//



#pragma mark --------- 转让群管理

#import "TransferGroupOwnerViewController.h"
#import "TransferGroupOwnerCell.h"

@interface TransferGroupOwnerViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTableView
 */
@property (nonatomic , strong) UITableView *myTableView;

/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
@end

@implementation TransferGroupOwnerViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择新群主";
    [self setUpNav];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpNav {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClick)];
   
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 56, 26)];
    [rightButton setTitle:@"完成" forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    rightButton.backgroundColor = [UIColor primaryColor];
    rightButton.layer.cornerRadius = 5;
    rightButton.layer.masksToBounds = YES;
    [rightButton addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
}
- (void)leftButtonClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightButtonClick {
    NSString *identifier = nil;
    for (Friends *model in self.dataArr) {
        if (model.isSelect) {
            identifier = model.memberNum;
        }
    }
    @weakify(self);
    [[AlertAction sharedInstance] showAlertWithTitle:nil message:@"确定选择XX为新群主?" confirmBtnTitle:@"确定" confirmBlock:^{
        [[TIMGroupManager sharedInstance] modifyGroupOwner:self.groupId user:identifier succ:^{
            @strongify(self);
            [self toastWithContent:@"转让成功"];
        } fail:^(int code, NSString *msg) {
            @strongify(self);
            [self toastWithContent:msg];
        }];
    } cancelBlock:nil];
}
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
}

- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void) initAction {
    
}

- (void)initData {
    //获取群成员数据
    [self.groupMemberArr enumerateObjectsUsingBlock:^(TGroupMemberCellData * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        Friends *model = [[Friends alloc] init];
        model.isSelect = NO;
        model.nickName = obj.name;
        model.memberNum = obj.identifier;
        model.headImage = obj.avatar;
        [self.dataArr addObject:model];
    }];
    [self.myTableView reloadData];

}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TransferGroupOwnerCell *cell = [TransferGroupOwnerCell cellWithTableView:tableView];
    
    if (self.dataArr.count > indexPath.row) {
        cell.model = self.dataArr[indexPath.row];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.dataArr enumerateObjectsUsingBlock:^(Friends *model, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == indexPath.row) {
            model.isSelect = !model.isSelect;
        } else {
            model.isSelect = NO;
        }
    }];
    [self.myTableView reloadData];
}
#pragma mark - target action

#pragma mark - private method

#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.rowHeight = 70;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
    }
    return _myTableView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
@end
